package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.maiji.cloud.entities.login.UserInfo;
import com.maiji.cloud.entities.shopingmall.*;
import com.maiji.cloud.mapper.*;
import com.maiji.cloud.microservice.UserInfoService;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.ResMetaData;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.service.*;
import com.maiji.cloud.utils.BaseService;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CapitalIOServiceImpl extends ServiceImpl<CapitalIOMapper, CapitalIOEntity> implements CapitalIOService {

    @Autowired
    private UserInfoService userInfoService;

    @Override
    public BaseMetaResDto<List<CapitalIOEntity>> findAllWithdrawalRecords(String start, List<Date> inTimes, String userId, String nickName, ReqMetaData metaData) {
        EntityWrapper<CapitalIOEntity> entityWrapper = new EntityWrapper<>();
        if (StringUtil.isBlank(nickName) && StringUtil.isNotBlank(userId)) entityWrapper.eq("user_id", userId);
        entityWrapper.eq("type", 2);
        entityWrapper.eq("start", start);
        if (inTimes != null && inTimes.size() > 0) {
            entityWrapper.ge("in_time", inTimes.get(0));
            if (inTimes.size() > 1) entityWrapper.le("in_time", inTimes.get(1));
        }
        List<UserInfo> userInfoList = Lists.newArrayList();
        if (StringUtil.isNotBlank(nickName)) {
            userInfoList = userInfoService.selectList(nickName);
            List<String> userIds = userInfoList.parallelStream().map(UserInfo::getUserId).collect(Collectors.toList());
            if (userIds.size() > 0) entityWrapper.in("user_id", userIds);
        }
        entityWrapper.orderBy("in_time", false);
        Page<CapitalIOEntity> page = selectPage(new Page<>(metaData.getPage(), metaData.getSize()), entityWrapper);
        List<CapitalIOEntity> capitalIOList = page.getRecords();
        if (userInfoList.size() == 0) {
            List<String> userIds = capitalIOList.parallelStream().map(CapitalIOEntity::getUserId).collect(Collectors.toList());
            userInfoList = userInfoService.selectList(userIds);
        }
        capitalIOList = BaseService.dealWithOneToOne(capitalIOList, "getUserId", CapitalIOEntity.class,
                "setUserInfo", userInfoList, "getUserId", UserInfo.class);
        ResMetaData resMetaData = new ResMetaData(page.getCurrent(), page.getSize(), page.getTotal());
        return new BaseMetaResDto<List<CapitalIOEntity>>(Status.SUCCESS).setData(capitalIOList).setResMetaData(resMetaData);
    }

}
